package com.meixiang.view.photoSelectActivity.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.meixiang.R;
import com.meixiang.global.GlobalType;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.photoSelectActivity.util.Bimp;
import com.meixiang.view.photoSelectActivity.util.BitmapCache;
import com.meixiang.view.photoSelectActivity.util.ImageItem;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/14 10:51
 */
public class PhotoShowFileAdapter extends RecyclerView.Adapter<PhotoShowFileAdapter.ViewHolder> {
    final String TAG = getClass().getSimpleName();
    /**
     * 当前分类所有图片
     */
    private ArrayList<ImageItem> dataList;
    /**
     * 已选择图片集合
     */
    private ArrayList<ImageItem> selectedDataList;

    private Context context;

    BitmapCache cache;

    private boolean isCheckout=false;
    private Intent mIntent;
    private int type=0;
    private int imageSelect;

    public PhotoShowFileAdapter(Context context, ArrayList<ImageItem> dataList, ArrayList<ImageItem> selectedDataList) {
        cache = new BitmapCache();
        this.context = context;
        this.dataList = dataList;
        this.selectedDataList = selectedDataList;

    }


    public void setType(int type) {
        this.type = type;
    }

    public void setImageSelect(int imageSelect) {
        this.imageSelect = imageSelect;
    }

    @Override
    public PhotoShowFileAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.list_item_photo_album, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;


    }

    @Override
    public void onBindViewHolder(final PhotoShowFileAdapter.ViewHolder holder, int position) {

        ImageItem bean = dataList.get(position);
        final ImageItem item = dataList.get(position);

        holder.mImageView.setTag(item.imagePath);


        cache.displayBmp(holder.mImageView, item.thumbnailPath, item.imagePath,
                callback);

        //判断是否有已选择状态
        if(selectedDataList.size()>0){
            for(int i=0;i<selectedDataList.size();i++){
                if(dataList.get(position).getImageId().equals(selectedDataList.get(i).getImageId())){
                    dataList.get(position).setSelected(true);
                    isCheckout=true;
                    holder.mCbIntegral.setBackgroundResource(R.drawable.selector_checkbox_up_bg);
                }
            }
        }
        if (bean != null) {
            holder.mRlButton.setTag(position);
            holder.mCbIntegral.setTag(position);

//            ((ViewHolder) holder).mCbIntegral.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    int position = (Integer)  ((ViewHolder) holder).mCbIntegral.getTag();
//                    if (dataList != null && mOnItemClickListener != null
//                            && position < dataList.size()) {
//                            mOnItemClickListener.onItemClick(((ViewHolder) holder).mCbIntegral, position,((ViewHolder) holder).mCbIntegral.isChecked() );
//                    }
//                }
//            });
            holder.mRlButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (Integer) holder.mRlButton.getTag();
                    if (item.isSelected == false) {
                        if(type==1){
                            if(imageSelect>=GlobalType.ORDER_PICTURE_COUNT){
                                AbToastUtil.showToast(context, "最多只能选择"+GlobalType.ORDER_PICTURE_COUNT+"张");
                                return;
                            }
                        }else{
                            if (Bimp.tempSelectBitmap.size() >= 8 ) {
                                AbToastUtil.showToast(context, "超出可选范围！");
                                return;
                            }
                        }
//                        isCheckout=true;
                        item.setSelected(true);
                        holder.mCbIntegral.setBackgroundResource(R.drawable.selector_checkbox_up_bg);
                    }else if(item.isSelected==true){
//                        isCheckout=false;
                        item.setSelected(false);
                        holder.mCbIntegral.setBackgroundResource(R.drawable.selector_checkbox_default_bg);
                    }
                    if (dataList != null && mOnItemClickListener != null && position < dataList.size()) {
                        mOnItemClickListener.onItemClick( holder.mRlButton, position,item.isSelected);
                    }
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
//        void onItemClick(View view, int position, boolean isChecked, CheckBox chooseBt);

        void onItemClick(RelativeLayout mRlButton, int position, boolean isChecked);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //当前显示的图片
        @Bind(R.id.image_view)
        ImageView mImageView;
        //点击选择的按钮
        @Bind(R.id.cb_integral)
        CheckBox mCbIntegral;

        @Bind(R.id.rl_button)
        RelativeLayout mRlButton;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }


    BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
        @Override
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params) {
            if (imageView != null && bitmap != null) {
                String url = (String) params[0];
                if (url != null && url.equals((String) imageView.getTag())) {
                    ((ImageView) imageView).setImageBitmap(bitmap);
                } else {
                    Log.e(TAG, "callback, bmp not match");
                }
            } else {
                Log.e(TAG, "callback, bmp null");
            }
        }
    };


}
